package com.fh.modules.ums.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.common.util.ConvertUtils;
import com.fh.common.web.AbstractController;
import com.fh.modules.ums.entity.UmsMember;
import com.fh.modules.ums.entity.UmsMemberPartnerInfo;
import com.fh.modules.ums.model.MemberModel;
import com.fh.modules.ums.service.IUmsMemberPartnerInfoService;
import com.fh.modules.ums.service.IUmsMemberService;
import com.fh.modules.ums.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fuhao	
 * @date 2019年10月4日
 * @description 用户表 前端控制器
 */
@RestController
@Api("合伙人表")
public class UmsMemberController extends AbstractController{
	@Autowired
	private UmsMemberService umsMemberService;

	@Autowired
	private IUmsMemberService iUmsMemberService;

	@Autowired
	private IUmsMemberPartnerInfoService umsMemberPartnerInfoService;

	@GetMapping(value = "/ums/umsMember/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="商城会员分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(UmsMember umsMember,
								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<UmsMember> queryWrapper = new QueryWrapper<UmsMember>();
		umsMember.setDelFlag(CommonConstant.DEL_FLAG_0);
		QueryWrapperUtil.initQueryWrapper(queryWrapper, umsMember);
		Page<UmsMember> page = new Page<UmsMember>(pageNo, pageSize);
		IPage<UmsMember> pageList = umsMemberService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

    @GetMapping(value = "/ums/umsMember/queryBuddy")
    @ApiOperation("分页列表查询伙伴")
    @AutoLog(value="分页列表查询伙伴",logType=CommonConstant.LOG_TYPE_QUERY)
    public Result queryBuddy(Integer partnerId,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
	    UmsMember umsMember=new UmsMember();
        umsMember.setPrePartnerId(partnerId);
        umsMember.setPartnerType(2);
        umsMember.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapper<UmsMember> queryWrapper = new QueryWrapper<UmsMember>();
        QueryWrapperUtil.initQueryWrapper(queryWrapper, umsMember);
        Page<UmsMember> page = new Page<UmsMember>(pageNo, pageSize);
        IPage<UmsMember> pageList = umsMemberService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

	@GetMapping(value = "/ums/member/queryMemberInfo")
	@ApiOperation("微信端获取合伙人信息")
	@AutoLog(value = "微信端获取合伙人信息", logType = CommonConstant.LOG_TYPE_QUERY)
	public Result queryMemberInfo(@RequestParam(name = "memberId") Integer memberId,String shareUrl) {
		MemberModel memberModel= iUmsMemberService.getMemberInfo(memberId);
		memberModel.setPartnerQrCode(shareUrl);
		return Result.ok(memberModel);
	}

	@GetMapping(value = "/ums/member/queryUmsMemberInfo")
	@ApiOperation("微信端获取合伙人信息-废弃")
	@AutoLog(value = "微信端获取合伙人信息-废弃", logType = CommonConstant.LOG_TYPE_QUERY)
	public Result queryUmsMember(@RequestParam(name = "openId") String openId) {
		UmsMember umsMember = umsMemberService.getOne(new QueryWrapper<UmsMember>().eq("third_account", openId));
		return Result.ok(umsMember);
	}

	@GetMapping(value = "/ums/member/queryMemberSimpleInfo")
	@ApiOperation("微信端获取用户一般信息")
	@AutoLog(value = "微信端获取用户一般信息", logType = CommonConstant.LOG_TYPE_QUERY)
	public Result queryMemberSimpleInfo(Integer memberId){
		return Result.ok(iUmsMemberService.querySimpleInfo(memberId));
	}


	@RequestMapping(value = "/ums/member/queryUmsMember", method = RequestMethod.GET)
	@ApiOperation("获取合伙人信息")
	@AutoLog(value = "通过用户名获取合伙人信息", logType = CommonConstant.LOG_TYPE_QUERY)
	public Result queryCurrentUser(@RequestParam(name = "username", required = true) String username) {
		UmsMember umsMember = umsMemberService.getMemberByName(username);
		return Result.ok(umsMember);
	}
	
	
	@RequestMapping(value = "/ums/member/add", method = RequestMethod.POST)
	@ApiOperation("新增商城会员表")
	@AutoLog("新增商城会员表")
	public Result<UmsMember> add(@RequestBody JSONObject jsonObject) {
		Result<UmsMember> result = new Result<UmsMember>();
		try {
			UmsMember member=JSON.parseObject(jsonObject.toString(), UmsMember.class);
			if(member.getId()==null){
				member.setDelFlag(0);
				if(member.getPrePartnerId()!=null){
					UmsMemberPartnerInfo umsMemberPartnerInfo=umsMemberPartnerInfoService.getById(member.getPrePartnerId());
					if(umsMemberPartnerInfo!=null){
						umsMemberPartnerInfo.setTeamPersonCount(umsMemberPartnerInfo.getTeamPersonCount()+1);
						umsMemberPartnerInfoService.updateById(umsMemberPartnerInfo);
						member.setPartnerType(2);
					}else{
						member.setPartnerType(0);
						member.setPrePartnerId(null);
					}
				}
			}

			umsMemberService.saveOrUpdate(member);
			result.setResult(member);
			result.success("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}
	
	@RequestMapping(value = "/ums/member/editSelf", method = RequestMethod.PUT)
	@ApiOperation("修改会员信息")
	@AutoLog("修改会员信息")
	public Result editSelf(@RequestBody JSONObject jsonObject) {
		UmsMember user = JSON.parseObject(jsonObject.toJSONString(), UmsMember.class);
		umsMemberService.updateById(user);
		return Result.ok("修改成功");
	}

}
